package com.ainote.service.impl;



import com.ainote.service.ExportService;
import com.itextpdf.text.Document;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.stereotype.Service;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class ExportServiceImpl implements ExportService {

    private final String exportDir = System.getProperty("java.io.tmpdir") + "/exports/";

    public ExportServiceImpl() {
        new File(exportDir).mkdirs();
    }

    private String now() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    @Override
    public File exportWord(String title, String summary, String fullText) throws Exception {
        String filename = exportDir + "MeetingSummary_" + System.currentTimeMillis() + ".docx";
        XWPFDocument doc = new XWPFDocument();

        // 封面
        XWPFParagraph titlePara = doc.createParagraph();
        titlePara.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun titleRun = titlePara.createRun();
        titleRun.setText("会议纪要");
        titleRun.setFontSize(26);
        titleRun.setBold(true);

        XWPFParagraph meta = doc.createParagraph();
        meta.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun metaRun = meta.createRun();
        metaRun.setText("生成时间：" + now());
        metaRun.setFontSize(12);

        doc.createParagraph().createRun().addBreak();

        // 摘要部分
        XWPFParagraph summaryTitle = doc.createParagraph();
        summaryTitle.createRun().setText("会议摘要");
        summaryTitle.getRuns().get(0).setBold(true);
        doc.createParagraph().createRun().setText(summary);

        // 全文部分
        XWPFParagraph textTitle = doc.createParagraph();
        textTitle.createRun().setText("会议全文记录");
        textTitle.getRuns().get(0).setBold(true);
        doc.createParagraph().createRun().setText(fullText);

        try (FileOutputStream fos = new FileOutputStream(filename)) {
            doc.write(fos);
        }

        return new File(filename);
    }

    @Override
    public File exportPdf(String title, String summary, String fullText) throws Exception {
        String filename = exportDir + "MeetingSummary_" + System.currentTimeMillis() + ".pdf";

        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        document.open();

        Font titleFont = new Font(Font.FontFamily.HELVETICA, 20, Font.BOLD);
        Font bodyFont = new Font(Font.FontFamily.HELVETICA, 12);

        document.add(new Paragraph("会议纪要", titleFont));
        document.add(new Paragraph("生成时间：" + now(), bodyFont));
        document.add(new Paragraph("\n"));

        document.add(new Paragraph("【会议摘要】", bodyFont));
        document.add(new Paragraph(summary, bodyFont));
        document.add(new Paragraph("\n"));

        document.add(new Paragraph("【会议全文】", bodyFont));
        document.add(new Paragraph(fullText, bodyFont));

        document.close();

        return new File(filename);
    }
}
